package com.example.testpic;

import java.util.List;

import com.example.testpic.BitmapCache.NativeImageCallBack;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class AlbumListAdapter extends BaseAdapter{
	
	private Context context;
	private List<ImageBucket> albumList;
	private BitmapCache cache;
	private ViewGroup viewGroup;

	public AlbumListAdapter(Context context,List<ImageBucket> imageBucketList,ViewGroup viewGroup){
		this.context = context;
		albumList = imageBucketList;
		this.viewGroup = viewGroup;
		cache = BitmapCache.getCacheInstance(context);
	}
	
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return (albumList != null)?albumList.size():0;
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return albumList.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		ViewHolder viewHolder = null;
		if (convertView == null) {
			viewHolder = new ViewHolder();
			convertView = LayoutInflater.from(context).inflate(R.layout.pop_list_item, null);
			viewHolder.albumDirImageView = (ImageView) convertView.findViewById(R.id.imageView);
			viewHolder.albumDirInfoTextView = (TextView) convertView.findViewById(R.id.tv_albumname);
			viewHolder.haveImageSelectedView = (ImageView) convertView.findViewById(R.id.have_image_selected);
			convertView.setTag(viewHolder);
		}else {
			viewHolder = (ViewHolder) convertView.getTag();
		}
		ImageBucket imageBucket = (ImageBucket) getItem(position);
		viewHolder.albumDirInfoTextView.setText(imageBucket.bucketName + " (" + imageBucket.imageList.size() + ")");
		List<ImageItem> imageItemList = imageBucket.imageList;
		boolean haveImageSelectedTag = false;
		for (ImageItem imageItem : imageItemList) {
			String imagePath = imageItem.imagePath;
			if (AlbumListActivity.map.containsKey(imagePath)) {
				haveImageSelectedTag = true;
				break;
			}
		}
		if (haveImageSelectedTag) {
			viewHolder.haveImageSelectedView.setVisibility(View.VISIBLE);
		} else {
			viewHolder.haveImageSelectedView.setVisibility(View.INVISIBLE);
		}
		if (imageItemList != null && imageItemList.size() > 0) {
			String sourceImagePath = imageItemList.get(0).imagePath;
			viewHolder.albumDirImageView.setTag(sourceImagePath);
			Bitmap bitmap = cache.loadBitmap(viewHolder.albumDirImageView, sourceImagePath, new NativeImageCallBack() {
				
				@Override
				public void onImageLoader(Bitmap bitmap, String path) {
					// TODO Auto-generated method stub
					ImageView imageView = (ImageView) viewGroup.findViewWithTag(path);
					if (imageView != null && bitmap != null) {
						imageView.setImageBitmap(bitmap);
					}
				}
			});
			if (bitmap != null) {
				viewHolder.albumDirImageView.setImageBitmap(bitmap);
			}
		}else {
			viewHolder.albumDirImageView.setImageBitmap(null);
		}
		return convertView;
	}
	
	class ViewHolder{
		ImageView albumDirImageView;
		TextView albumDirInfoTextView;
		ImageView haveImageSelectedView;
	}

}
